package com.jxsd.edu.cn.tools;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

/**
 * 爬取特定的手机信息
 * */
public class CelInfoCrawler {

	public HashMap<String,String> crawling(String url) 
	{
		HashMap<String,String>  map=new  HashMap<String,String>();
		Parser   parse=new  Parser();
		Example  e=new  Example();
		String   inputHTML=e.getHtml(url);
		
		try 
		{
			parse.setInputHTML(inputHTML);
			
			//取出所有的具有class="param"的ul标签
			NodeFilter   filter=new  HasAttributeFilter("class","param");
			NodeList      nodes=parse.extractAllNodesThatMatch(filter);
			if(nodes!=null)
			{
				Node  node=nodes.elementAt(0);
				NodeList  children=node.getChildren();
				for(int i=2;i<children.size()-1;i++)
				{
					Node  n=children.elementAt(i);
					String  str=n.toPlainTextString();
					
					String  s=str.replace("：", ":");
					
					int  first=s.indexOf(":");
					
					String attributeName=s.substring(0,first);
					String value=s.substring(first+1, s.length()-1);
					map.put(attributeName,value);
				}
			}
			
		} catch (ParserException e1)
		{
			e1.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
        return  map;
	}

}
